package com.shop.user.atom.atomsvc.impl;

import com.shop.common.model.ServiceBean;
import com.shop.common.model.param.user.AddressInput;
import com.shop.common.model.result.user.AddressOutput;
import com.shop.common.tmpl.BaseService;
import com.shop.common.util.state.AddressState;
import com.shop.user.atom.dao.AddressMapper;
import com.shop.user.atom.atomsvc.IAddressAtomSvc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressAtomSvc extends BaseService implements IAddressAtomSvc {
    @Autowired
    AddressMapper addressMapper;

    @Override
    public ServiceBean<String> insertAddress(AddressInput addressInput) {
        int num = addressMapper.insertSelective(addressInput);
        if (opSuccess(num)) {
            return resultSuccess("新建地址成功");
        }
        return resultFailed("新建地址失败");
    }

    @Override
    public ServiceBean<AddressOutput> selectAddress(AddressInput addressInput) {
        List<AddressOutput> list = addressMapper.selectBySelective(addressInput);
        if (list == null || list.size() == 0) {
            return resultFailed("查询地址失败");
        }
        return resultModel(true, "查询成功", list.get(0));
    }

    @Override
    public ServiceBean<List<AddressOutput>> selectAddressList(AddressInput addressInput) {
        List<AddressOutput> list = addressMapper.selectBySelective(addressInput);
        if (list == null || list.size() == 0) {
            return resultFailed("查询地址失败");
        }
        return resultModel(true, "查询成功", list);
    }

    @Override
    public ServiceBean<String> updateAddress(AddressInput addressInput) {
        int num = addressMapper.updateByPrimaryKeySelective(addressInput);
        if (opSuccess(num)) {
            return resultSuccess("修改成功");
        }
        return resultFailed("修改失败");
    }

    @Override
    public ServiceBean<String> deleteAddressById(List<Integer> param) {
        int num = addressMapper.deleteByIds(param);
        if (opSuccess(num)) {
            return resultSuccess("修改成功");
        }
        return resultFailed("修改失败");
    }

    @Override
    public ServiceBean<String> deleteAddressByUser(Integer userId) {

        return null;
    }

    @Override
    public Integer selectAddressCount(AddressInput address) {
        return addressMapper.selectAddressCount(address);
    }

    @Override
    public ServiceBean settingDefault(AddressInput address) {
        AddressInput address_t = new AddressInput();
        address_t.setUserId(address.getUserId());
        address_t.setStatus(AddressState.USED.value());
        List<AddressOutput> addresses = addressMapper.selectBySelective(address_t);
        if (addresses != null && addresses.size() > 0) {
            AddressOutput addressOutput = addresses.get(0);
            if (addressOutput.getId().intValue() == address.getId().intValue()) {
                return resultFailed("该地址已经是默认地址，无需修改");
            }
            address_t.setId(addressOutput.getId());
            address_t.setStatus(AddressState.WAIT.value());
            addressMapper.updateByPrimaryKeySelective(address_t);
        }
        address.setStatus(AddressState.USED.value());
        int i = addressMapper.updateByPrimaryKeySelective(address);
        if (opSuccess(i)) {
            return resultSuccess();
        }
        return resultFailed();
    }
}
